﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Text.RegularExpressions;

namespace PizzaWic.customer
{
    public partial class EditAccount : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack){
            SqlAccess sql = new SqlAccess();
            //DataSet data =  sql.getOneCustomer(int.Parse(Session["id_customer"].ToString()));
            DataSet data = sql.getOneCustomer(1);
            this.name.Text = (string)data.Tables[0].Rows[0][0];
            this.surname.Text = (string)data.Tables[0].Rows[0][1];
            this.login.Text = (string)data.Tables[0].Rows[0][2];
            this.phoneNumber.Text = data.Tables[0].Rows[0][3]+"";
            this.mail.Text = (string)data.Tables[0].Rows[0][4];
            this.number.Text = data.Tables[0].Rows[0][5]+"";
            this.street.Text = (string)data.Tables[0].Rows[0][6];
            this.city.Text = (string)data.Tables[0].Rows[0][7];
            this.area_code.Text = data.Tables[0].Rows[0][8]+"";

            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
            if (!this.ValidPhone(this.phoneNumber.Text))
            {
                this.notification.Text = "Veuillez entrer un numéro de téléphone valide.";
            }
            else
            {
                if (!this.ValidMail(this.mail.Text))
                {
                    this.notification.Text = "Veuillez entrer une adresse mail valide.";
                }
                else
                {
                    this.editUser();
                }
            }
        }

        private void editUser()
        {
            String passwordCrypted = FormsAuthentication.HashPasswordForStoringInConfigFile(this.password.Text, "MD5");
            SqlAccess sqlAccess = new SqlAccess();
            Object[] data = new Object[11];

            //  data[0] = Session["id_customer"];
            data[0] = 2;
            data[1] = this.name.Text;
            data[2] = this.surname.Text;
            data[3] = this.login.Text;
            data[4] = passwordCrypted;
            data[5] = int.Parse(this.phoneNumber.Text);
            data[6] = this.mail.Text;
            data[7] = int.Parse(this.number.Text);
            data[8] = this.street.Text;
            data[9] = this.city.Text;
            data[10] = int.Parse(this.area_code.Text);
            sqlAccess.updateCustomer(data);
        }

        private bool ValidMail(string adresse)
        {
            System.Text.RegularExpressions.Regex myRegex = new Regex(@"^([\w]+)@([\w]+)\.([\w]+)$");
            //([\w]+) ==> caractère alphanumérique apparaissant une fois ou plus 
            return myRegex.IsMatch(adresse); // retourne true ou false selon la vérification
        }

        private bool ValidPhone(string phone)
        {
            System.Text.RegularExpressions.Regex myRegex = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
            //([\w]+) ==> caractère alphanumérique apparaissant une fois ou plus 
            return myRegex.IsMatch(phone); // retourne true ou false selon la vérification
        }
    }
}